package cn.edu.xaut.bean;

import java.util.HashMap;
import java.util.Map;

public class RegisterFormBean {
    private String name;
    private String password;
    private String password2;
    private String email;

    private Map<String,String> errors=new HashMap<String,String>();
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getPassword2() {
        return password2;
    }

    public void setPassword2(String password2) {
        this.password2 = password2;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    @Override
    public String toString() {
        return "RegisterFormBean{" +
                "name='" + name + '\'' +
                ", password='" + password + '\'' +
                ", password2='" + password2 + '\'' +
                ", email='" + email + '\'' +
                '}';
    }

    public boolean validate(){
        boolean flag= true;
        if(name==null||name.trim().equals("")){ //trim()去掉字符串两端的空格
            errors.put("name","用户名不能为空");
            flag=false;
        }
        if(password==null||password.trim().equals("")){
            errors.put("password","密码不能为空");
            flag=false;
        } else if (password.length()<6||password.length()>16){
            errors.put("password","密码长度必须在6-16之间");
            flag=false;
        }

        if(password!=null&&!password.equals(password2)){
            errors.put("password2","两次密码不一致");
            flag=false;
        }

        if(email==null||email.trim().equals("")){
            errors.put("email","邮箱不能为空");
            flag=false;
        } else if (!email.matches("[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\\.[a-zA-Z0-9_-]+)+")){
            errors.put("email","邮箱格式不正确");
            flag=false;
        }
        return flag;
    }

    public Map<String,String> getErrors(){
        return errors;
    }

    public void setErrors(String err,String errMsg){
        if((err!=null)&&(errMsg!=null))
            errors.put(err,errMsg);
    }
}
